import java.util.Comparator;

public class PCmp implements Comparator<Object> {
    public int compare(Object p0, Object p1) {
        if (p0 instanceof Number) {
            if (p1 instanceof Number)) {
                return ((Integer) p0).intValue() - ((Integer) p1).intValue();
            }
            // All other patterns (at this moment) are more general than
            // constants.
            return true;
        } else if (p0 instanceof PVar) {
            return p1 instanceof PVar;
        } else if (p0 instanceof PQuery) {
            return p1 instanceof PQuery;
        }
        return false;
    }
    
    public boolean equal(Object p) {
        if (p0 instanceof Number) {
            return p1 instanceof Number &&
                ((Integer) p0).intValue() == ((Integer) p1).intValue();
            }
        } else if (p0 instanceof PVar) {
            return p1 instanceof PVar && ((PVar) p0).sameVar((PVar) p1);
        } else if (p0 instanceof PQuery) {
            return p0 == p1;
        }
        return false;                       
    }
}
